package draw

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"vrcm/src/vrcm/app/admin/models/draw"
	service "vrcm/src/vrcm/app/admin/service/draw"
	dto "vrcm/src/vrcm/app/admin/service/dto/draw"
	"vrcm/src/vrcm/common/apis"
)

type Draw struct {
	apis.Api
}

func (e Draw) GetPage(c *gin.Context) {
	req := dto.DrawBasicGetPageReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]draw.DrawBasic, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取抽奖失败，失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e Draw) SaveDraw(c *gin.Context) {
	req := dto.DrawSaveReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveDrawBasic(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存SaveDraw失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存成功")
}

func (e Draw) GetDraw(c *gin.Context) {
	req := dto.GetDrawReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err, data := s.GetDrawBasic(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取抽奖失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

func (e Draw) GetDrawing(c *gin.Context) {
	req := dto.GetDrawingReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err, data := s.GetDrawingBasic(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取正在抽奖失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

func (e Draw) DeleteDraw(c *gin.Context) {
	req := dto.DrawDeleteReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.DeleteDrawBasic(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除抽奖失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "删除成功")
}

func (e Draw) StartDraw(c *gin.Context) {
	req := dto.StartDrawReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StartDraw(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("开始抽奖失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "开始成功")
}

func (e Draw) EndDraw(c *gin.Context) {
	req := dto.EndDrawReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.EndDraw(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("停止抽奖失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "停止成功")
}

func (e Draw) CopyDraw(c *gin.Context) {
	req := dto.DrawCopyReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.CopyDrawBasic(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("复制抽奖失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "复制成功")
}

func (e Draw) SaveClaimPrize(c *gin.Context) {
	req := dto.ClaimPrizeSaveReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SaveClaimPrize(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存领奖页面失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存领奖页面成功")
}

func (e Draw) SavePrizeConfig(c *gin.Context) {
	req := dto.PrizeConfigSaveReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SavePrizeConfig(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存奖项配置失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存奖项配置成功")
}

func (e Draw) CopyPrizeConfig(c *gin.Context) {
	req := dto.PrizeConfigCopyReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.CopyPrizeConfig(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("复制奖项配置失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "复制成功")
}

func (e Draw) DeletePrizeConfig(c *gin.Context) {
	req := dto.PrizeConfigDeleteReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.DeletePrizeConfig(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("复制奖项配置失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "复制成功")
}

func (e Draw) ClickDraw(c *gin.Context) {
	req := dto.ClickDrawReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.ClickDraw(&req)
	if err != nil {
		if err.Error() == string(apis.RepeatDrawError.Code) {
			e.Error(apis.RepeatDrawError.Code, err, fmt.Sprintf("点击开始抽奖失败，失败信息 %s", "不能重复参与抽奖"))
		} else {
			e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("点击开始抽奖失败，失败信息 %s", err.Error()))
		}

		return
	}
	e.OK(nil, "点击开始抽奖成功")
}

func (e Draw) SaveAwardInfo(c *gin.Context) {
	req := dto.SaveAwardInfoReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SaveAwardInfo(&req)
	if err != nil {
		e.Error(apis.UpdatePlatformError.Code, err, fmt.Sprintf("保存中奖信息失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "保存中奖信息成功")
}

func (e Draw) GetAwardInfo(c *gin.Context) {
	req := dto.GetAwardInfoReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err, data := s.GetAwardInfo(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取抽奖信息失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "获取抽奖信息成功")
}

func (e Draw) GetUserAwardInfo(c *gin.Context) {
	req := dto.GetUserAwardInfoReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err, data := s.GetUserAwardInfo(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取用户抽奖信息失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "获取用户抽奖信息成功")
}

func (e Draw) AddDrawWhites(c *gin.Context) {
	req := dto.AddDrawWhiteReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.AddDrawWhites(&req)
	if err != nil {
		if err.Error() == string(apis.DrawWhiteNumError.Code) {
			e.Error(apis.DrawWhiteNumError.Code, err, fmt.Sprintf("添加抽奖白名单信息失败， %s", "白名单获奖人数不得超过奖品数"))

		} else {
			e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("添加抽奖白名单信息失败，失败信息 %s", err.Error()))
		}
		return
	}
	e.OK(nil, "添加抽奖白名单信息成功")
}

func (e Draw) SaveDrawWhite(c *gin.Context) {
	req := dto.SaveDrawWhiteReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SaveDrawWhite(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("保存抽奖白名单信息失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存抽奖白名单信息成功")
}

func (e Draw) DeleteDrawWhite(c *gin.Context) {
	req := dto.DeleteDrawWhiteReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.DeleteDrawWhite(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("删除抽奖白名单信息失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "删除抽奖白名单信息成功")
}

func (e Draw) GetDrawWhite(c *gin.Context) {
	req := dto.GetDrawWhiteReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetDrawWhite(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取抽奖白名单信息失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "获取抽奖白名单信息成功")
}

func (e Draw) SavePrizePage(c *gin.Context) {
	req := dto.SavePrizePageReq{}
	s := service.DrawService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SavePrizePage(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存领奖页面失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.DrawId, "保存领奖页面成功")
}
